package cn.tiancaifan.micro.controller.system.basic;


import cn.tiancaifan.micro.entity.Department;
import cn.tiancaifan.micro.entity.SystemResults;
import cn.tiancaifan.micro.service.DepartmentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/system/basic/dep")
public class DepartmentController {

    @Autowired
    DepartmentService departmentService;

    @GetMapping("/")
    public SystemResults getAllDepartment(){
        return SystemResults.ok(null,departmentService.getAllDepartment());
    }

    @PostMapping("/")
    public SystemResults addDep(@RequestBody Department dep) {
        departmentService.addDep(dep);
        if (dep.getResult() == 1) {
            return SystemResults.ok("添加成功", dep);
        }
        return SystemResults.error("添加失败");
    }
    @DeleteMapping("/{id}")
    public SystemResults deleteDepById(@PathVariable Integer id) {
        Department dep = new Department();
        dep.setId(id);
        departmentService.deleteDepById(dep);
        if (dep.getResult() == -2) {
            return SystemResults.error("该部门下有子部门，删除失败");
        } else if (dep.getResult() == -1) {
            return SystemResults.error("该部门下有员工，删除失败");
        } else if (dep.getResult() == 1) {
            return SystemResults.ok("删除成功");
        }
        return SystemResults.error("删除失败");
    }
}
